import request from '@/http';
import { message } from 'antd';

export default class UserManagerService {
  static async queryUserList(queryParams, pageValue, pageSizeValue) {
    const result: any = await request.post('/api/getUserListByPager', {
      ...queryParams,
      pageIndex: pageValue - 1,
      pageSize: pageSizeValue,
    });
    if (result.success) {
      const data = result.data.map((user) => ({
        key: `user_${user.id}`,
        id: user.id,
        userName: user.userName,
        userNo: user.userNo,
        description: user.description,
        sex: user.sex,
        createTime: user.createTime,
        updateTime: user.updateTime,
      }));
      return {
        current: pageValue,
        pageSize: pageSizeValue,
        total: result.totalCount,
        dataSource: data,
      };
    }
    return null;
  }

  static async doCreateOrUpdateUser(data) {
    const result: any = await request.post('/api/addOrUpdateUser', {
      ...data,
    });
    if (result.success) {
      message.success('用户添加成功');
      return true;
    }
    return false;
  }

  static async doDeleteUser(id) {
    try {
      const result: any = await request.get('/api/removeUserById?id=' + id);
      if (result.success) {
        message.success('用户删除成功');
        return true;
      }
    } catch (e) {
      message.error(e.data.message);
    }

    return false;
  }
}
